<?php
namespace Home\Controller;
use Common\Controller\HomeBaseController;
use Common\Model\ProDaiModel;
use Think\Verify;
use Common\Model\MemberModel;
use Think\Model;
use Common\Model\ArticleModel;
use Common\Model\JiluModel;

class IndexController extends HomeBaseController {
	/*
	 * 网站首页
	 */
	public function index($tj = NULL) {
		$member = new MemberModel();
		//判断当前链接是否为推荐链接
		if (!empty($tj)){//如果是推广链接，储存推荐码
			cookie('tj',$tj);
		}
		//个人信息
		if(MID){
			$this->info = $member->where("id = ".MID)->find();
		}
		//文章列表
		$article = new ArticleModel();
		//首页大图
		$this->banner = $article->where("cate = 10 and status = 1")->select();	
		$this->display();
	}
	/*
	 * 密码找回
	 */
	public function findpwd() {
		if(IS_AJAX){
			$data = I('post.');
			
			if (empty($data['phoneverify']) || $data['phoneverify'] != S($data['mobile'])) {
				$this->error('手机验证码不正确');
			}
			$model_M = new MemberModel();
			$info = $model_M->where("account='".$data['account']."'")->find();
			if ($info['mobile'] != $data['mobile']) {
				$this->error('帐号与手机号不匹配');
			}
			if (false !== $model_M->where('id='.$info['id'])->setField('password',pwd_hash($data['password']))) {
				S($data['mobile'],null);//验证码清空
				$this->success('密码重置成功', U('Index/login') );
			}else {
				$this->error($model_M->getError());
			}
		}else {
			if(is_login()){
				$this->redirect('Mem/index');
			}else {
				$this->display();
			}
		}
	}
	/*
	 * 用户登录
	 */
	public function login($account = null, $password = null, $verify = null) {
		if(IS_POST){
			if(empty($account)) $this->error('请填写登录帐号！');
			if (empty($password)) $this->error('请填写登录密码！');
			
			$map = array();
			$map['account']	= $account;
			$map["status"]	= array("gt",-1); //-1:删除 0:禁用 1:正常
			$map['password'] = $password;
			$map['logscontent'] = get_client_ip(1);
			$model_M = new MemberModel();
			
			if (!$model_M->login($map)) {
				$this->error($model_M->getError());
			}

			$user = session('user_auth');
			if($user['cate'] == 1){
				$this->redirect("Shang/lists");
			}elseif($user['cate'] == 2){
				$this->redirect("Shua/lists");
			}else {
				$this->redirect("Index/index");
			}
		}else {
			if(is_login()){
				$this->redirect('Index/index');
			}else {
				$this->display();
			}
		}
	}
	/*
	 * 用户注册
	 */
	public function register() {
		if(IS_POST){
			$data = I('post.');
			$data['password'] = pwd_hash($data['password']);
			$data['reg_time'] = time();
			$tj = cookie('tj');
			if (!empty($tj)){//如果存在推荐码
				$data['parent_id'] = $tj;
			}
			$model_M = new MemberModel();
			if (false !== $model_M->myReg($data)) {
				$user = session('user_auth');
				$this->redirect("Index/duanxin");
			}else {
				$this->error($model_M->getError());
			}
		}else {
			if(is_login()){
				$this->redirect(C('LOGIN_URL'));
			}else {
				$this->display();
			}
		}
	}
	/*
	 * 用户短信认证
	 */
	public function duanxin(){
		$member = new MemberModel();
		if (IS_POST){
			$data = I('post.');
			$verify_M = new Verify();
			$verify_M->reset = false;
			if (!$verify_M->check($data['verify'], 1)) {//验证码编号为1
				$this->error('验证码输入错误！');
			}
				
			if (empty($data['phoneverify']) || $data['phoneverify'] != S($data['mobile'])) {
				$this->error('手机验证码不正确');
			}
			//修改用户状态
			if(false == $member->where("id=".MID)->setField("status",0)){
				$this->error("手机号验证失败");
			}else{
				$user = session('user_auth');
				if($user['cate'] == 1){
					$this->redirect("Shang/index");
				}elseif($user['cate'] == 2){
					$this->redirect("Shua/index");
				}else {
					$this->redirect("Index/index");
				}	
			}
		}else {
			$this->display();
		}
	}
	/*
	 * 图片验证码
	 */
	public function verify() {
		$verify = new Verify();
		$verify->entry(1);//验证码编号为1
	}
	/**
	 * 注销接口
	 */
	function logout() {
		if(is_login()) {
			D('Member')->logout();
			session('[destroy]');
			$this->redirect("Index/index");
		} else {
			$this->redirect("Index/index");
		}
	}
	/**
	 * 发送验证码
	 * @param $phone 电话号码
	 * @param $check 0-不检测 1-验证用户存在 2-验证用户不存在
	 */
	public function sendVerify($phone,$check='0') {
		if (preg_match('/^1\d{10}$/',$phone)===1) {
			if ($check>0) { //验证用户
				$model = new MemberModel();
				$info = $model->where("`mobile`='$phone'")->find();
				if ($check=='1' && empty($info) && $info['status']!=1) {//验证用户存在
					$this->error('该手机号不存在或被禁用');
				}
				if ($check=='2' && !empty($info)) {//验证用户不存在
					$this->error('该手机号已被注册');
				}
			}
			$options['accountsid'] = C('UCPAAS_SID');
			$options['token'] = C('UCPAAS_TOKEN');
			$code = mt_rand(1000,9999);
			$param = $code.',10'; //验证码,有效时间10分钟
			S($phone,$code,10*60); //验证码缓存到文件
			/*
			 * string '{"resp":{"respCode":"000000","templateSMS":{"createDate":"20150702175710","smsId":"ec7d3495aa01d6fe76de20e3dcc13723"}}}'
			*
			* */
			$ucpass = new \Common\Lib\Ucpaas($options);
			$ucpass->templateSMS(C('UCPAAS_APPID'),$phone,C('UCPAAS_TEMPID'),$param);
			//$this->error($rs);
			$this->success('发送成功');
		}else {
			$this->error('请输入正确的电话号码');
		}
	}
	/*
	 * 推荐返佣
	*/
	public function tuijian(){
		$member = new MemberModel();
		
		$map = array();
		$map['status'] = 1;
		$map['parent_id'] = MID;
		$list = $member->where($map)->select();
		//list数据处理
		foreach ($list as &$v) {
			$v['time_type'] =date('Y-m-d H:i:s',$v['reg_time']);
		}
		unset($v);
		$this->assign("list",$list);//获取当前登录用户推荐的已进行实名认证的会员
		//判断当前登录用户类型
		$user = session('user_auth');
		if($user['cate'] == 1){
			$this->display('tuijian1');
		}elseif ($user['cate'] == 2){
			$this->display('tuijian2');
		}
	}
	/*
	 * 我的返现记录
	*/
	public function fanxian(){
		$jilu = new JiluModel();
		$member = new MemberModel();
		//提现总额
		$this->txzong = $jilu->where("cate = 2 and sid = ".MID)->sum("money");
		//账户信息
		$this->info = $member->where("id = ".MID)->find();
		//佣金总额
		$this->zyongjin = $jilu->where("cate = 5 and sid = ".MID)->sum("money");
		
		$map = array();
		$map['cate'] = array("between",array(6,7));
		$map['sid'] = MID;
		$list = $jilu->where($map)->select();
		//list数据处理
		foreach ($list as &$v) {
			$v['time_type'] =date('Y-m-d H:i:s',$v['createtime']);
		}
		unset($v);
		$this->assign("list",$list);
		//判断当前登录用户类型
		$user = session('user_auth');
		if($user['cate'] == 1){
			$this->display('fanxian1');
		}elseif ($user['cate'] == 2){
			$this->display('fanxian2');
		}
	}
}